#include "Arduino.h"
#include <NESState.h>

#ifdef _LIBNES_SETUP_LOOP_DEFINED

const int LED_PIN = 13;

const int CLOCK_PIN = A0;
const int STROBE_PIN = A1;
const int DATA_PIN = A2;

static NESState nesState(DATA_PIN, STROBE_PIN, CLOCK_PIN);

void setup()
{
  Serial.begin(9600);

  Serial.println("NES signals tester");

  pinMode(LED_PIN, OUTPUT);

  //Serial.println("setup() [exit]");
}

void loop()
{
  //Serial.println("loop() - [enter]");

  if (nesState.refresh())
  {
    digitalWrite(LED_PIN, HIGH);

    // consume the saved state and reset
    if (nesState.buttonIsPressed(NESState::A))      Serial.print('A');
    if (nesState.buttonIsPressed(NESState::B))      Serial.print('B');
    if (nesState.buttonIsPressed(NESState::UP))     Serial.print('^');
    if (nesState.buttonIsPressed(NESState::DOWN))   Serial.print('V');
    if (nesState.buttonIsPressed(NESState::LEFT))   Serial.print('<');
    if (nesState.buttonIsPressed(NESState::RIGHT))  Serial.print('>');
    if (nesState.buttonIsPressed(NESState::SELECT)) Serial.print("[SEL]");
    if (nesState.buttonIsPressed(NESState::START))  Serial.print("[START]");

    Serial.println("");

    delay(10);

    digitalWrite(LED_PIN, LOW);
  }

  //delay(1000);
  //Serial.println("loop() - [exit]");
}

#endif	// _LIBNES_SETUP_LOOP_DEFINED
